package com.aegean.MonitorExamSYS.monitor.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.monitor.business.MonitorBusiness;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.opensymphony.xwork2.ActionSupport;

@Controller("MonitorLogin")
@Scope("prototype")
public class MonitorLogin extends ActionSupport implements ServletRequestAware{
	@Resource
	MonitorBusiness business;
	private int unit_Id;
	private MonitorValue monitor;
    private HttpServletRequest request;
    private HttpSession session;
	public int getUnit_Id() {
		return unit_Id;
	}

	public void setUnit_Id(int unit_Id) {
		this.unit_Id = unit_Id;
	}

	public MonitorValue getMonitor() {
		return monitor;
	}

	public void setMonitor(MonitorValue monitor) {
		this.monitor = monitor;
	}

	@Override
	public void validate() {
		// TODO Auto-generated method stub
		if (monitor.getMonitor_Identity() == null || monitor.getMonitor_Identity().length() == 0) {
			this.addFieldError("monitor.monitor_Identity", "对不起，员工号不能为空！");
		}
		if(monitor.getUser_Password()== null ||monitor.getUser_Password().length() == 0){
			this.addFieldError("monitor.user_Password", "对不起，用户密码不能为空！");
		}
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		monitor = business.loginMonitor(monitor, unit_Id);
		if (monitor == null) {
			return INPUT;
		} else {
			session.setAttribute("MonitorLogin", monitor);
			return SUCCESS;
		}
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request=arg0;
		session=request.getSession();
		
	}

}
